package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.service.AppUserInfoService;
import cn.stylefeng.guns.modular.system.service.ApplyAddMoneyService;
import cn.stylefeng.guns.modular.system.utils.ResultVOUtil;
import cn.stylefeng.guns.modular.system.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

import javax.naming.event.ObjectChangeListener;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApplyAddMoneyController 存款记录
 * @Author lzy
 * @Date 2020/2/6 16:50
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/depositRecord")
public class ApplyAddMoneyController {

    private final static Logger log = LoggerFactory.getLogger(ApplyAddMoneyController.class);

    private static final String SECCESS = "SECCESS";

    private String PREFIX = "/modular/system/depositRecord/";

    @Autowired
    private ApplyAddMoneyService applyAddMoneyService;

    @Autowired
    private AppUserInfoService appUserInfoService;
    /**
     * 跳转到主页面
     *
     * @author wg
     * @Date 2019-06-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "depositRecord.html";
    }

    /**
     *
     * @param startTime 订单提交时间头
     * @param endTime   订单提交时间尾
     * @param userOrder 根据订单查找
     * @param status    1待审核，2审核中
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findDepositRecordAll")
    @ResponseBody
    public ResultVO findDepositRecordAll(@RequestParam(value = "startTime", required = false) String startTime,
                                         @RequestParam(value = "endTime", required = false)String endTime,
                                         @RequestParam(value = "userOrder", required = false)String userOrder,
                                         @RequestParam(value = "status", required = false)Integer status,
                                         @RequestParam("page")Integer pageNum, @RequestParam("limit") Integer pageSize){
        Long id = LoginContextHolder.getContext().getUserId();
        log.info("代理人id"+id);
        Assert.isTrue(!ObjectUtils.isEmpty(id), ResultEnum.PROXYID_DOES_NOT_EXIST.getMessage());
        //每个代理只能查询出自己旗下的user用户的存取款记录，不能查询出代理下面的代理的user用户
        List<String> userIds = appUserInfoService.findUserIdsByProxyId(id.toString());
        if(userIds.size() == 0){
            return ResultVOUtil.success(SECCESS);
        }
        //设置默认分页参数
        PageHelper.startPage(pageNum,pageSize);
        List<Map<Object,Object>> syss = applyAddMoneyService.findList(startTime,endTime,status,userIds);
        PageInfo<Map<Object,Object>> pageInfo = new PageInfo<>(syss);
        return ResultVOUtil.success(pageInfo.getList(),pageInfo.getTotal());
    }



}
